//
//  ViewController.m
//  Hpnotiq
//
//  Created by Harmandeep Singh on 21/05/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "ViewController.h"
#import <MobileCoreServices/UTCoreTypes.h>
#import "FrameSelectionViewController.h"




//temp imports
#import "ModifyPhotoViewController.h"




@interface ViewController()

- (IBAction)newPhotoClicked:(id)sender;
- (IBAction)libraryImagesClicked:(id)sender;

- (IBAction)tempModifyScreen:(id)sender;

@end


@implementation ViewController

@synthesize imagePickerPopover = _imagePickerPopover;

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    
    
    
    self.title = @"Choose Photo";
    
    self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:nil] autorelease];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
	[super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
	[super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown);
}

- (void)dealloc {
    
    [_imagePickerPopover release];
    [super dealloc];
}






#pragma mark -
#pragma mark Action Methods



- (IBAction)newPhotoClicked:(id)sender
{
    
#if TARGET_IPHONE_SIMULATOR
    
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"New photo cannot be shot in simulator as it does not have any camera." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
    [alertView show];
    [alertView release];
    
#else
    
    
    
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    {
        UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
        imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
        imagePickerController.delegate = self;
        imagePickerController.mediaTypes = [NSArray arrayWithObjects:(NSString *)kUTTypeImage, nil];
        [self presentModalViewController:imagePickerController animated:YES];
        [imagePickerController release];
    }
    else
    {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"No camera available on this device." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [alertView show];
        [alertView release];
    }
    
    
    
#endif
    
}




- (IBAction)libraryImagesClicked:(id)sender
{
    UIButton *button = (UIButton *)sender;
    
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    imagePickerController.delegate = self;
    imagePickerController.mediaTypes = [NSArray arrayWithObjects:(NSString *)kUTTypeImage, nil];
    
    
    self.imagePickerPopover = [[[UIPopoverController alloc] initWithContentViewController:imagePickerController] autorelease];
    [self.imagePickerPopover presentPopoverFromRect:button.frame
                                             inView:self.view 
                           permittedArrowDirections:UIPopoverArrowDirectionUp 
                                           animated:YES];
    
    
    [imagePickerController release];
}



- (IBAction)tempModifyScreen:(id)sender
{
    ModifyPhotoViewController *viewController = [[ModifyPhotoViewController alloc] initWithNibName:@"ModifyPhotoViewController" bundle:nil];
    viewController.mOriginalImage = [UIImage imageNamed:@"flower29.jpg"];
    [self.navigationController pushViewController:viewController animated:YES];
    [viewController release];
}


#pragma mark -
#pragma mark UIPickerView Delegate Methods

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
	if([[info valueForKey:@"UIImagePickerControllerMediaType"] isEqualToString:(NSString *)kUTTypeImage])
	{
        
        UIImage *image = [info valueForKey:@"UIImagePickerControllerOriginalImage"];
        
        
        
        
        BOOL shouldPresentNextScreenAnimated = YES;
        BOOL imageIsShotFromApp = NO;
        
        
        if(picker.sourceType == UIImagePickerControllerSourceTypePhotoLibrary)
        {
            [self.imagePickerPopover dismissPopoverAnimated:YES];
            
            shouldPresentNextScreenAnimated = YES;
            imageIsShotFromApp = NO;
        }
        else if(picker.sourceType == UIImagePickerControllerSourceTypeCamera)
        {
            [picker dismissModalViewControllerAnimated:YES];
            
            shouldPresentNextScreenAnimated = NO;
            imageIsShotFromApp = YES;
        }
        

        
        
        FrameSelectionViewController *viewController = [[FrameSelectionViewController alloc] initWithNibName:@"FrameSelectionViewController" bundle:nil];
        viewController.mOriginalImage = image;
        viewController.mImageIsShotFromApp = imageIsShotFromApp;
        [self.navigationController pushViewController:viewController animated:shouldPresentNextScreenAnimated];
        [viewController release];
        
	}
	
    
	
}



- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    if(picker.sourceType == UIImagePickerControllerSourceTypeCamera)
    {
        [picker dismissModalViewControllerAnimated:YES];
    }
}








@end
